home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 April: Mac OS SDK / Dev.CD Apr 00 SDK1.toast / Development Kits / Mac OS / Open Transport 1.3 / Open Transport SDK / Open Tpt Client Developer / Samples / Internet / OTUdpPitchSample.cp < prev    next >
Encoding:
Text File  |  1998-04-30  |  8.2 KB  |  382 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OTUdpPitchSample.cp
  3.  
  4.     Contains:    UDP pitch sample code
  5.  
  6.     Copyright:    © 1993-1997 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. // OT UDP Pitch Test Program (as an SIOW app)
  12.  
  13. #include <QuickDraw.h>
  14. #include <stdio.h>
  15. #include <StdLib.h>
  16. #include <TextUtils.h>
  17. #include <strings.h>
  18. #include <String.h>
  19. #include <Events.h>
  20. #include <Menus.h>
  21. #include <Devices.h>
  22. #include <Windows.h>
  23. #include <OpenTransport.h>
  24. #include <OpenTptInternet.h>
  25.  
  26.  
  27. /*******************************************************************************
  28. ** GLOBAL VARIABLES
  29. ********************************************************************************/
  30.  
  31. #define kMaxDataLen 256
  32.  
  33. InetPort    gCatchPort=0;
  34. InetHost    gCatchIpAddr=0;
  35. InetPort      gPitchPort=0;
  36. InetHost    gPitchIpAddr=0;
  37.  
  38. UInt32        gNoOfTimes=0;
  39. UInt16        gBindCompleted = 0;
  40.  
  41. OTEventCode    gCode;
  42. TEndpoint*    gCookie;
  43. OSStatus    gErr;
  44.  
  45. struct InetAddress reqsin, retsin, tmpsin;
  46. char data[kMaxDataLen];
  47. char defaultData[] = "Go Cal, beat Stanford !!!";
  48.  
  49. Boolean    gFirstTime = true;
  50.  
  51. /*******************************************************************************
  52. ** Function Prototypes
  53. ********************************************************************************/
  54.  
  55. void        Inits();
  56. void        CleanUp();
  57. void        Idle();    
  58. void        DoIt();
  59. OSStatus    DoStaticBind(TEndpoint* ep);
  60. OSStatus    DoSend(TEndpoint* ep);
  61.  
  62.  
  63. /*******************************************************************************
  64. **  main function
  65. ********************************************************************************/
  66.  
  67. void main(int, char**) 
  68. {
  69.     char userInput[256];
  70.  
  71.     do 
  72.     {
  73.         Inits();
  74.         DoIt();
  75.         CleanUp();
  76.         
  77.         fprintf(stderr, "Again (y/n)?\n");
  78.         gets(&userInput[0]);
  79.     } while ( userInput[0] == 'y' || userInput[0] == 'Y' );
  80.     
  81.     fprintf(stderr, "Bye\n");
  82.     exit (0);
  83. }
  84.  
  85. /*******************************************************************************
  86. ** Initialize Quickdraw and ASLM
  87. ********************************************************************************/
  88.  
  89. void Inits()
  90. {
  91.     if ( gFirstTime == true )
  92.     {
  93.         InitGraf(&qd.thePort);
  94.         gFirstTime = false;
  95.     }
  96.  
  97.     if ( InitOpenTransport() != kOTNoError )
  98.     {
  99.         fprintf(stderr, "OTUdpPitch: Could not initialize Open Transport, exiting\n");
  100.         exit(1);
  101.     }
  102.     
  103. }
  104.  
  105. /*******************************************************************************
  106. ** Clean up at the end
  107. ********************************************************************************/
  108.  
  109. void CleanUp()
  110. {
  111.     CloseOpenTransport();
  112. }
  113.  
  114. /*******************************************************************************
  115. ** Idle
  116. ********************************************************************************/
  117.  
  118. void Idle()
  119. {
  120.     SystemTask();
  121. }
  122.  
  123. /*******************************************************************************
  124. ** IsPressed
  125. ********************************************************************************/
  126.  
  127. Boolean IsPressed(UInt16 k, KeyMap map)
  128. {
  129.     UInt8* keyMap    = (UInt8*)map;
  130.     return (keyMap[k >> 3] >> (k & 7) & 1);
  131. }
  132.  
  133. /*******************************************************************************
  134. ** CmdKey
  135. ********************************************************************************/
  136.  
  137. Boolean UserAbort()
  138. {
  139.     KeyMap    keyMap;
  140.     
  141.     GetKeys(keyMap);
  142.     if ( IsPressed(0x37, keyMap) && 
  143.         (IsPressed(0x2F, keyMap) || IsPressed(0x41, keyMap)) )
  144.     {
  145.         FlushEvents(everyEvent, 0);    // needed so that "gets" gets not confused later on
  146.         return true;
  147.     }
  148.     else
  149.     {
  150.         return false;
  151.     }
  152. }
  153.  
  154.  
  155. /*******************************************************************************
  156. ** EventHandler
  157. ********************************************************************************/
  158.  
  159. pascal void EventHandler(void*, OTEventCode event, OTResult result, void* cookie)
  160. {
  161.     if ( event == T_BINDCOMPLETE )
  162.     {
  163.         gBindCompleted = 1;
  164.     }
  165.     else if ( event == T_OPENCOMPLETE )
  166.     {
  167.         gErr = (OSStatus) result;
  168.         gCookie = (TEndpoint*) cookie;
  169.         gCode = event;
  170.     }
  171.     return;
  172. }
  173.  
  174. /*******************************************************************************
  175. ** DoIt
  176. ********************************************************************************/
  177.  
  178. void DoIt()
  179. {
  180.     TEndpoint*        ep = NULL;
  181.     TEndpointInfo    info;
  182.     OSStatus        err=0;
  183.     long            myport=0;
  184.     InetHost        myaddr=0;
  185.     char            mystr[255];
  186.  
  187.     myport = 0;
  188.     fprintf(stderr, "What UDP port should I use to send a data ? (enter UDP port number)\n");
  189.     if (gets(mystr) != 0)
  190.     {
  191.         stringtonum(mystr, &myport);
  192.         gPitchPort =(InetPort)  myport;
  193.     }
  194.     myaddr = 0;
  195.     fprintf(stderr, "Where should I send a data ? (enter IP address)\n");
  196.     if (gets(mystr) != 0) 
  197.     {
  198.         if (OTInetStringToHost(mystr, &myaddr) == 0)
  199.         {
  200.             gCatchIpAddr = (InetHost) myaddr;
  201.         }
  202.     }
  203.     myport = 0;
  204.     fprintf(stderr, "To which port should I send a data ? (enter port number)\n");
  205.     if (gets(mystr) != 0)
  206.     {
  207.         stringtonum(mystr, &myport);
  208.         gCatchPort = (InetPort) myport;
  209.     }
  210.     fprintf(stderr, "What should I send ? (enter data string)\n");
  211.     if (gets(data) == 0) 
  212.     {
  213.         strcpy(data, defaultData);
  214.         fprintf(stderr, "send default data: <%s>\n", data);
  215.     }
  216.     myport = 0;
  217.     fprintf(stderr, "How many times should I send this data ?\n");
  218.     if (gets(mystr) != 0) 
  219.     {
  220.         stringtonum(mystr, &myport);
  221.         gNoOfTimes = (UInt16)myport;
  222.     }
  223.     
  224.     OTInetHostToString(gCatchIpAddr, mystr);
  225.     fprintf(stderr, "The program will send <%d> packets to <%s:%d> on port <%d>\n", gNoOfTimes, mystr, gCatchPort, gPitchPort);
  226.     OTInitInetAddress(&reqsin, gPitchPort, (InetHost) 0);
  227.  
  228.     do
  229.     {
  230.         //
  231.         // Now create a UDP
  232.         //
  233. #if 0
  234.         gCode = 0;
  235.         gCookie = NULL;
  236.         gErr = 0;
  237.         err = OTAsyncOpenEndpoint(OTCreateConfiguration(kUDPName), 0, 
  238.                                   &info, EventHandler, 0);
  239.         if ( err == 0 )
  240.         {
  241.             while ( gCode == 0 )
  242.                 OTIdle();
  243.             err = gErr;
  244.         }
  245.         if ( err != 0 )
  246.         {
  247.             ep = NULL;
  248.             fprintf(stderr,"ERROR: OpenEndpoint(\"UDP\") failed with %d\n", err);
  249.             break;
  250.         }
  251.         else
  252.         {
  253.             ep = gCookie;
  254.         }
  255. #else
  256.         ep = OTOpenEndpoint(OTCreateConfiguration(kUDPName), 0, &info, &err);
  257.  
  258.         if ( ep == NULL || err != kOTNoError )
  259.         {
  260.             ep = NULL;
  261.             fprintf(stderr,"ERROR: OpenEndpoint(\"UDP\") failed with %d\n", err);
  262.             break;
  263.         }
  264.         //
  265.         // Install notifier we're going to use for testing
  266.         //
  267.         err = ep->InstallNotifier(EventHandler, 0);
  268.         if ( err != kOTNoError )
  269.         {
  270.             fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
  271.             break;
  272.         }
  273. #endif
  274.         //
  275.         // Try to bind
  276.         // 
  277.         ep->SetAsynchronous();
  278.         err = DoStaticBind(ep);
  279.         if ( err != kOTNoError )
  280.             break;        
  281.  
  282.         ep->SetSynchronous();
  283.  
  284.         while ( gNoOfTimes-- && !UserAbort() )
  285.         {
  286.             err = DoSend(ep);
  287.             if ( err != kOTNoError )
  288.                 break;
  289.             Idle();
  290.         }
  291.         //
  292.         // Try to Unbind
  293.         // 
  294.         err = ep->Unbind();
  295.         if ( err != kOTNoError )
  296.         {
  297.             fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
  298.             break;
  299.         }
  300.     } while (false);
  301.  
  302.     if (ep)
  303.     {
  304.         //
  305.         // Remove notifier
  306.         //
  307.         ep->RemoveNotifier();
  308.         //
  309.         // Get rid of endpoint.
  310.         //
  311.         err = OTCloseProvider(ep);
  312.         if ( err != kOTNoError )
  313.         {
  314.             fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
  315.         }
  316.     }
  317. }
  318.  
  319. /*******************************************************************************
  320. ** DoStaticBind
  321. ********************************************************************************/
  322.  
  323. OSStatus DoStaticBind(TEndpoint* ep)
  324. {
  325.     TBind        req, ret;
  326.     OSStatus    err;
  327.  
  328.     // bind udp to current address and port
  329.     req.addr.len = sizeof(struct InetAddress);
  330.     req.addr.buf = (UInt8*) &reqsin;
  331.     req.qlen = 1;                                        // don't care for udp
  332.     ret.addr.maxlen = sizeof(struct InetAddress);
  333.     ret.addr.buf = (UInt8*) &retsin;
  334.  
  335.     err = ep->Bind(&req, &ret);
  336.     if ( err != kOTNoError )
  337.     {
  338.         fprintf(stderr, "Static Bind returns %d\n", err);
  339.         return err;
  340.     }
  341.     while ( gBindCompleted == 0 )
  342.     {
  343.     }
  344.     gBindCompleted = 0;
  345. //    gPitchIpAddr = reqsin.fPort;
  346.     return err;
  347. }
  348.  
  349. /*******************************************************************************
  350. ** DoSend
  351. ********************************************************************************/
  352.  
  353. OSStatus DoSend(TEndpoint* ep)
  354. {
  355.     OSStatus    err = kOTNoError;
  356.     TUnitData    unitdata;
  357.     char        mystr[255];
  358.  
  359.     // Send a UDP datagram
  360.  
  361.     OTInitInetAddress(&tmpsin, gCatchPort, gCatchIpAddr);
  362.     
  363.     unitdata.addr.len = sizeof(struct InetAddress);
  364.     unitdata.addr.buf = (UInt8*) &tmpsin;
  365.     unitdata.opt.len = 0;
  366.     unitdata.opt.buf = 0;
  367.     unitdata.udata.len = strlen(data);
  368.     unitdata.udata.buf = (UInt8*) data;
  369.     
  370.     err = ep->SndUData( &unitdata);
  371.     if ( err != kOTNoError )
  372.     {
  373.         fprintf(stderr, "SndUData() returns %d\n", err);
  374.     }
  375.     else 
  376.     {
  377.         OTInetHostToString(gCatchIpAddr, mystr);
  378.         fprintf(stderr, "Sent <%s> to <%s:%d>\n", data, mystr, gCatchPort);
  379.     }
  380.     return err;
  381. }
  382.